//
//  ViewController.m
//  20-JSONModel
//
//  Created by 曹曹 on 16/8/10.
//  Copyright © 2016年 曹曹. All rights reserved.
//

#import "ViewController.h"
#import "GXAllCar.h"
#import "GXList.h"
#import "GXResult.h"
#import "GXBrandlist.h"
#import "CustomTableViewCell.h"

@interface ViewController ()
@property (nonatomic, strong) GXAllCar *allCar;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self handleAllCarData];
}
#pragma mark - AllCar Data
- (void)handleAllCarData {
    NSDictionary *dictionary = [self JSONObjectWithResource:@"AllCar" ofType:@"json"];
    _allCar = [GXAllCar modelWithDictionary:dictionary];
    
}
#pragma mark - JSON
- (nullable id)JSONObjectWithResource:(NSString *)resource ofType:(NSString *)type {
    NSString *path = [[NSBundle mainBundle] pathForResource:resource ofType:type];
    NSData *data = [NSData dataWithContentsOfFile:path];
    NSError *error = nil;
    return [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error];
}
#pragma mark - tableView 
#pragma mark - section title
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    return _allCar.result.brandlist[section].letter;
}
#pragma mark - rows
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _allCar.result.brandlist[section].list.count;
}
#pragma mark - sections
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return _allCar.result.brandlist.count;
}
#pragma mark - cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    CustomTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reuse"];
    cell.list = _allCar.result.brandlist[indexPath.section].list[indexPath.row];
    return cell;
}
#pragma mark - Other
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
